//
//  JJMainController.m
//  MyAnimations
//
//  Created by jxf on 15/12/22.
//  Copyright © 2015年 JJ.sevn. All rights reserved.
//

#import "JJMainController.h"
#import "JJCellItem.h"
#import "JJTableViewCell.h"
#import "JJSnowViewController.h"
#import "JJQiqiuController.h"
#import "JJSpringAnimationViewController.h"
#import "JJPlaneViewController.h"
#import "JJShowPictureViewController.h"
#import "JJLoadAnimationController.h"
@interface JJMainController ()
@property(nonatomic, strong) NSMutableArray * cellItemsArray;
@end

@implementation JJMainController

#pragma mark -
#pragma mark - lazy load
- (NSMutableArray *)cellItemsArray
{
    if (!_cellItemsArray) {
        _cellItemsArray = [NSMutableArray array];
    }
    return _cellItemsArray;
}

#pragma mark -
#pragma mark - initTableViewStyle
- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

#pragma mark -
#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCellItem];
}

- (void)setupCellItem
{
    JJCellItem *item = [JJCellItem cellItemWithTitle:@"snow"];
    item.operation = ^(){
        
        JJSnowViewController *snowVc = [[JJSnowViewController alloc] init];
        [self presentViewController:snowVc animated:YES completion:nil];
    };
    
    JJCellItem *item1 = [JJCellItem cellItemWithTitle:@"fly"];
    item1.operation = ^(){
        
        JJQiqiuController *qiqiuVc = [[JJQiqiuController alloc] init];
        [self presentViewController:qiqiuVc animated:YES completion:nil];
    };
    
    JJCellItem *item2 = [JJCellItem cellItemWithTitle:@"Spring"];
    item2.operation = ^(){
        
        JJSpringAnimationViewController *springVc = [[JJSpringAnimationViewController alloc] init];
        [self presentViewController:springVc animated:YES completion:nil];
    };
    
    JJCellItem *item3 = [JJCellItem cellItemWithTitle:@"plane"];
    item3.operation = ^(){
        
        JJPlaneViewController *planeVc = [[JJPlaneViewController alloc] init];
        [self presentViewController:planeVc animated:YES completion:nil];
    };
    
    JJCellItem *item4 = [JJCellItem cellItemWithTitle:@"showPicture"];
    item4.operation = ^(){
        
        JJShowPictureViewController *showPictureVc = [[JJShowPictureViewController alloc] init];
        [self presentViewController:showPictureVc animated:YES completion:nil];
    };
    
    JJCellItem *item5 = [JJCellItem cellItemWithTitle:@"loadView"];
    item5.operation = ^(){
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:NSStringFromClass([JJLoadAnimationController class]) bundle:nil];
        JJLoadAnimationController *loadVc = [storyBoard instantiateInitialViewController];
        [self presentViewController:loadVc animated:YES completion:nil];
    };
    
    [self.cellItemsArray addObjectsFromArray:@[item, item1, item2, item3, item4, item5]];
}
#pragma mark -
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellItemsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JJTableViewCell *cell = [JJTableViewCell tableViewCellWithTableView:tableView];
    JJCellItem *item = self.cellItemsArray[indexPath.row];
    cell.item =item;
    
    return cell;
}

#pragma mark -
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    JJTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    JJCellItem *item = cell.item;
    if (item.operation) {
        item.operation();
    }
}
@end
